package com.dure.hotmemory.server;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.dure.commonlib.util.FileUtils;

import com.dure.hotmemory.R;
import com.dure.hotmemory.notification.EppNotificationControl;
import com.socks.library.KLog;
import com.thin.downloadmanager.DownloadManager;
import com.thin.downloadmanager.DownloadRequest;
import com.thin.downloadmanager.DownloadRequest.Priority;
import com.thin.downloadmanager.DownloadStatusListener;
import com.thin.downloadmanager.ThinDownloadManager;

public class MyService extends IntentService {

    private ThinDownloadManager downloadManager;
    String downloadPath;
    private static final int DOWNLOAD_THREAD_POOL_SIZE = 4;
    /**
     */
//    private static final String FILE1 = "http://app.suning.com/d.php?pack=com.suning.mobile.epa";

    MyDownloadListner myDownloadStatusListener = new MyDownloadListner();

    int downloadId1 = 0;
    private DownloadRequest request1;

    EppNotificationControl notificationControl;

    public MyService() {
        super("MyService");
    }

    Context context;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        context = this;
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String url = intent.getStringExtra("url");
        String name = intent.getStringExtra("name");
        int iconId = intent.getIntExtra("img_id", R.mipmap.ic_launcher);
        int notifyId = intent.getIntExtra("notify_id", 0);

        downloadPath = FileUtils.createSDCardDir(name + ".apk");
        notificationControl = new EppNotificationControl(url, name, iconId, downloadPath,notifyId, context);
        downloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE);
        initDownload(url);
        if (downloadManager.query(downloadId1) == DownloadManager.STATUS_NOT_FOUND) {
            downloadId1 = downloadManager.add(request1);
        }
        notificationControl.showProgressNotify();

    }

    private void initDownload(String url) {
        Uri downloadUri = Uri.parse(url);
        Uri destinationUri = Uri.parse(downloadPath);
        request1 = new DownloadRequest(downloadUri)
                .setDestinationURI(destinationUri).setPriority(Priority.HIGH)
                .setDownloadListener(myDownloadStatusListener);

    }

    class MyDownloadListner implements DownloadStatusListener {

        @Override
        public void onDownloadComplete(int id) {
            if (id == downloadId1) {
                KLog.i("lesleixz", "download completed");
            }
        }

        @Override
        public void onDownloadFailed(int id, int errorCode, String errorMessage) {

            if (id == downloadId1) {
                KLog.i("lesleixz", "DownloadFailed" + errorMessage);
            }
        }

        @Override
        public void onProgress(int id, long totalBytes, long downloadedBytes,
                int progress) {

            if (id == downloadId1) {
                KLog.i("lesleixz", progress + "");
                notificationControl.updateNotification(progress);
            }

        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        KLog.i("lesleixz", "onDestroy");
        super.onDestroy();
    }
}
